const promise = require('bluebird');
const utils = promise
    .promisifyAll(require('/Users/Steve/tools/myGit/xNode/common/FooUtils.js'));

// const token = process.argv[2].trim();

const userHome = '/Users/Steve';
const cloudHome = userHome + '/cloud';

const alfred_srcFile = userHome + '/tools/alfred/';
const alfred_targetFile = cloudHome + '/tools/alfred/';

const sublime_srcFile = userHome + '/tools/sublimeText';
const sublime_targetFile = cloudHome + '/tools/sublimeText';

const idea_keymaps_srcFile = `${userHome}/Library/Preferences/IntelliJIdea2016.2/keymaps`;
const idea_keymaps_targetFile = `${cloudHome}/tools/IntelliJIdea/keymaps`;

const alfred_workflow_urls_backupSql = '/usr/local/mysql/bin/mysqldump -u root -proot mysql alfred_workflow_urls>' + alfred_targetFile + 'alfred_workflow_urls.sql';
const react_native_props_backupSql = '/usr/local/mysql/bin/mysqldump -u root -proot mysql react_native_props>' + alfred_targetFile + 'react_native_props.sql';

const osx_enviroments_srcFile = userHome + '/.bash_profile';
const osx_enviroments_targetFile = cloudHome + '/tools/osxEnviroments';


utils.cleanDirAsync(alfred_targetFile).then(()=> {
    return utils.copyAsync(alfred_srcFile, alfred_targetFile);
}).then(()=> {
    return utils.execCommandAsync(alfred_workflow_urls_backupSql);
}).then(()=> {
    return utils.execCommandAsync(react_native_props_backupSql);
}).then(()=> {
    return utils.cleanDirAsync(sublime_targetFile);
}).then(()=> {
    return utils.copyAsync(sublime_srcFile, sublime_targetFile);
}).then(()=> {
    return utils.cleanDirAsync(idea_keymaps_targetFile);
}).then(()=> {
    return utils.copyAsync(idea_keymaps_srcFile, idea_keymaps_targetFile);
}).then(()=> {
    return utils.cleanDirAsync(osx_enviroments_targetFile);
}).then(()=> {
    return utils.execCommandAsync(`cp ${osx_enviroments_srcFile} ${osx_enviroments_targetFile}`);
}).then(() => {
    console.log('all backup finish success.');
}).catch((err) => {
    console.log(`error is:${err}`);
});

